<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetMemberRoles;

// 配置
use App\Base\Config\BaseConfig;
use App\Http\Config\AssetFileConfig;
use App\Plugins\FcpApi\TweetFiles\TweetFilesConfig;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class FsConfig extends BaseConfig
{
    // 主表
    const CFG_TABLE = 'member_roles';

    // 主表额外搜索字段
    const ADDED_SEARCHABLE_FIELDS = [

    ];

    const TYPE_OPTION = [
        ['key' => 1, 'text' => '管理人员类'],
        ['key' => 2, 'text' => '系统设置类'],
        ['key' => 3, 'text' => '用户运营类'],
    ];

    //默认权限
    const DEFAULT_PERMISSION = '[{"permKey":"content_view","permValue":true,"permStatus":""},{"permKey":"dialog","permValue":true,"permStatus":""},{"permKey":"post_publish","permValue":true,"permStatus":""},{"permKey":"post_review","permValue":false,"permStatus":""},{"permKey":"post_email_verify","permValue":false,"permStatus":""},{"permKey":"post_phone_verify","permValue":false,"permStatus":""},{"permKey":"post_prove_verify","permValue":false,"permStatus":""},{"permKey":"post_limit_status","permValue":false,"permStatus":""},{"permKey":"post_limit_type","permValue":1,"permStatus":""},{"permKey":"post_limit_period_start","permValue":"2021-08-01 22:30:00","permStatus":""},{"permKey":"post_limit_period_end","permValue":"2021-08-02 08:00:00","permStatus":""},{"permKey":"post_limit_cycle_start","permValue":"23:00:00","permStatus":""},{"permKey":"post_limit_cycle_end","permValue":"08:30:00","permStatus":""},{"permKey":"post_limit_rule","permValue":1,"permStatus":""},{"permKey":"comment_publish","permValue":true,"permStatus":""},{"permKey":"comment_review","permValue":false,"permStatus":""},{"permKey":"comment_email_verify","permValue":false,"permStatus":""},{"permKey":"comment_phone_verify","permValue":false,"permStatus":""},{"permKey":"comment_prove_verify","permValue":false,"permStatus":""},{"permKey":"comment_limit_status","permValue":false,"permStatus":""},{"permKey":"comment_limit_type","permValue":1,"permStatus":""},{"permKey":"comment_limit_period_start","permValue":"2021-08-01 22:30:00","permStatus":""},{"permKey":"comment_limit_period_end","permValue":"2021-08-02 08:00:00","permStatus":""},{"permKey":"comment_limit_cycle_start","permValue":"23:00:00","permStatus":""},{"permKey":"comment_limit_cycle_end","permValue":"08:30:00","permStatus":""},{"permKey":"comment_limit_rule","permValue":1,"permStatus":""},{"permKey":"post_editor_image","permValue":true,"permStatus":""},{"permKey":"post_editor_video","permValue":true,"permStatus":""},{"permKey":"post_editor_audio","permValue":true,"permStatus":""},{"permKey":"post_editor_doc","permValue":true,"permStatus":""},{"permKey":"comment_editor_image","permValue":true,"permStatus":""},{"permKey":"comment_editor_video","permValue":false,"permStatus":""},{"permKey":"comment_editor_audio","permValue":false,"permStatus":""},{"permKey":"comment_editor_doc","permValue":false,"permStatus":""},{"permKey":"images_max_size","permValue":5,"permStatus":""},{"permKey":"videos_max_size","permValue":50,"permStatus":""},{"permKey":"videos_max_time","permValue":15,"permStatus":""},{"permKey":"audios_max_size","permValue":50,"permStatus":""},{"permKey":"audios_max_time","permValue":60,"permStatus":""},{"permKey":"docs_max_size","permValue":10,"permStatus":""},{"permKey":"download_file_count","permValue":999,"permStatus":""}]';

    // model 使用 - 表单映射
    const FORM_FIELDS_MAP = [
        'id'        => 'id',
        'name'      => 'name',
        'nickname'  => 'nickname',
        'rank_num'  => 'rank_num',
        'is_enable' => 'is_enable',
        'remark'    => 'remark',
        'type'      => 'type',
        'icon_file_id'   => 'icon_file_id',
        'icon_file_url'  => 'icon_file_url',
        'more_json'  => 'more_json',
        'is_display_name'  => 'is_display_name',
        'is_display_icon'  => 'is_display_icon',
        'nickname_color'  => 'nickname_color',
        'permission'  => 'permission',
    ];

    // 新增规则
    public function storeRule()
    {
        $table = self::CFG_TABLE;
        $assetFileTable = TweetFilesConfig::CFG_TABLE;

        $rule = [
            'name'          => [
                'required',
                Rule::unique($table)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'nickname'      => [
                'filled',
                Rule::unique($table)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'rank_num'  => 'numeric',
            'file_id'   => Rule::exists($assetFileTable, 'id')->where(function ($query) {
                $query->where('deleted_at', null);
            }),
            'file_url'  => 'url',
            'more_json' => 'json',
        ];

        return $rule;
    }

    // 更新规则
    public function updateRule()
    {
        $id = request()->input('id');
        $table = self::CFG_TABLE;
        $assetFileTable = TweetFilesConfig::CFG_TABLE;

        $rule = [
            'id'    => [
                'required',
                Rule::exists($table)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'name'          => [
                'filled',
                Rule::unique($table)->ignore($id)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'nickname'          => [
                'filled',
                Rule::unique($table)->ignore($id)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'rank_num'      => 'numeric',
            'file_id'   => Rule::exists($assetFileTable, 'id')->where(function ($query) {
                $query->where('deleted_at', null);
            }),
            'file_url'  => 'url',
            'more_json' => 'json',
        ];

        return $rule;
    }
}
